package org.qengine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.qengine.dao.ClassDao;
import org.qengine.entity.ClassEntity;
import org.qengine.entity.StuInfoEntity;
import org.qengine.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;


@Service
public class ClassServiceImpl extends ServiceImpl<ClassDao, ClassEntity>
        implements ClassService {

    @Autowired
    private ClassDao classDao;


    @Override
    public ClassEntity getClassByID(Long classID) {
        return classDao.getClassByID(classID);
    }

    @Override
    public ClassEntity updateClass(ClassEntity classEntity) {
        // 检查是否存在相同的 classID 记录
        QueryWrapper<ClassEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classID", classEntity.getClassID());

        ClassEntity existingStuInfo = classDao.selectOne(queryWrapper);
        if (existingStuInfo != null) {
            // 如果存在，则更新记录
            classDao.update(classEntity, queryWrapper);
        } else {
            // 如果不存在，则插入新记录
            classDao.insert(classEntity);
        }
        return classEntity;

    }

    @Override
    public int deleteClass(Long classID) {
        return classDao.deleteClass(classID);
    }

    @Override
    public List<ClassEntity> getAllClass() {
        return classDao.getAllClass();
    }
}
